package com.snap.vseries.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.snap.vseries.AppConfig;
import com.snap.vseries.R;
import com.snap.vseries.base.BaseFragment;
import com.snap.vseries.bean.ExpInfo;
import com.snap.vseries.bean.ProgramSection;
import com.snap.vseries.bean.ProgramStage;
import com.snap.vseries.ui.ExpNewExperimentActivity;
import com.snap.vseries.ui.HomeActivity;
import com.snap.vseries.utils.AutoUtil;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

@SuppressLint("ValidFragment")
public class NewExperimentStartFragment extends BaseFragment {
    private final ExpInfo expInfo;
    @BindView(R.id.exp_name_tv)
    TextView expNameTv;
    @BindView(R.id.sample_name_tv)
    TextView sampleNameTv;
    @BindView(R.id.programs_tv)
    TextView programsTv;
    @BindView(R.id.plan_time_tv)
    TextView planTimeTv;
    @BindView(R.id.container)
    FrameLayout container;


    @SuppressLint("ValidFragment")
    public NewExperimentStartFragment(ExpInfo expInfo) {
        this.expInfo = expInfo;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setIndex();
        initView();
    }

    private float getTotalTime() {
        List<ProgramStage> list = expInfo.getProgram();
        float total = 1f;
        float lastTemp = 25f;
        for (int i = 0; i < list.size(); i++) {
            ProgramStage stage = list.get(i);
            for (int k = 0; k < stage.getCycleCount(); k++) {
                for (int j = 0; j < stage.getSections().size(); j++) {
                    ProgramSection section = stage.getSections().get(j);
                    total += section.getDuration();
                    float deltaTemp = section.getTemp() - lastTemp;
                    total += Math.abs(deltaTemp) / (deltaTemp > 0 ? AppConfig.INCREASE_VELOCITY : AppConfig.DECREASE_VELOCITY);
                    lastTemp = section.getTemp();
                }
            }
        }
        return total;
    }

    private void initView() {
        expNameTv.setText(expInfo.getName());
        sampleNameTv.setText(expInfo.getSampleName() == null ? "N/A" : expInfo.getSampleName());
        planTimeTv.setText(AutoUtil.getHourAndMin((long) getTotalTime()));
        programsTv.setText(expInfo.getTestProject());
        container.setVisibility(View.VISIBLE);
        container.removeAllViews();
        ProgramFragment programFragment = new ProgramFragment(expInfo);
        getChildFragmentManager().beginTransaction().add(R.id.container, programFragment).commitAllowingStateLoss();
    }

    @Override
    protected int layoutResId() {
        return R.layout.fragment_new_experiment_start;
    }


    @OnClick(R.id.tv_start_experiment)
    public void click(View view) {
        switch (view.getId()) {
            case R.id.tv_start_experiment:
                if (getActivity() != null) {
                    expInfo.setStartTime(System.currentTimeMillis());
                    Logger.i("设置实验:启动");
                    HomeActivity.startActivity(getContext());
                    ((ExpNewExperimentActivity) getActivity()).getController().startExp(expInfo);
                    if (mActivity != null) {
                        mActivity.finish();
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            setIndex();
            initView();
        }
    }


    private void setIndex() {
        ExpNewExperimentActivity activity = (ExpNewExperimentActivity) getActivity();
        Objects.requireNonNull(activity).index = 3;
    }
}
